package com.yz.jdd.receiver;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;


import com.yunzong.permissions.Permission;
import com.yunzong.permissions.XXPermissions;
import com.yz.jdd.base.MyApplication;
import com.yz.jdd.base.entity.Response_Base;
import com.yz.jdd.base.event.Events;
import com.yz.jdd.base.event.RxBus;
import com.yz.jdd.base.utils.BaseConfigs;
import com.yz.jdd.base.utils.Constants;
import com.yz.jdd.base.utils.LogUtils;
import com.yz.jdd.base.utils.PreferencesUtils;
import com.yz.jdd.network.ApiCallback;
import com.yz.jdd.network.NetWorkRequset;
import com.yz.jdd.network.request.Request_Update;
import com.yz.jdd.voice.OrderVoice;
import com.yz.jdd.voice.SpeechUtilOffline;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by lirui on 2018/8/31.
 * 上传推送平台注册ID
 */

public class Requst_Update_Utils {

    public static void requstUpdata(Context context) {
        Request_Update request_update = new Request_Update();
        request_update.setLat(PreferencesUtils.getString(context, BaseConfigs.STRING_LAT));
        request_update.setLon(PreferencesUtils.getString(context, BaseConfigs.STRING_LON));

        String xgId = PreferencesUtils.getString(context, "xgPushId", "");
        String gtId = PreferencesUtils.getString(context, "getuiPushId", "");
        String aliId = PreferencesUtils.getString(context, "aliPushId", "");
        String jgId = PreferencesUtils.getString(context, "jigPushId", "");

        request_update.setPushId(xgId);
        request_update.setAliPushId(aliId);
        request_update.setGetuiPushId(gtId);
        request_update.setJigPushId(jgId);
        String serialNum = android.os.Build.SERIAL;
        request_update.setPosSn(serialNum);
        NetWorkRequset.doUpdate(request_update, new ApiCallback() {
            @Override
            public void onSuccess(Response_Base response_baseModel) {

            }

            @Override
            public void onNeedLogin(String msg) {

            }

            @Override
            public void onFail(Response_Base response_baseModel) {

            }
        });
    }

    /**
     * 同步等待
     *
     * @param context
     * @param orderId
     * @param content
     */
    public static synchronized void sendNotification(Context context, String orderId, String content, int firstChannel, String payStatus, String ChineseChars) {
        String ids = "";
        if ("0".equals(payStatus)) {
            //从取消订单列表中
            ids = PreferencesUtils.getString(MyApplication.application, Constants.PRE_STRING_CANCEL_ORDERS, "");
        } else {
            //从支付订单中取
            ids = PreferencesUtils.getString(MyApplication.application, Constants.PRE_STRING_ORDERS, "");
        }

        if (!ids.contains(orderId)) {
            OrderVoice orderVoice = new OrderVoice();
            if (content != null && content.contains("商家")) {
                content = content.replace("商家", Constants.BRAND_VOICE);
            }
            Log.e("tfbm","=== 推送数据内容："+content);
            orderVoice.setContent(content);
            orderVoice.setFirstChannel(firstChannel);
            orderVoice.setReceiveTime(System.currentTimeMillis());
            orderVoice.setOrderId(orderId);
            orderVoice.setPayStatus(payStatus);
            orderVoice.setChineseChars(ChineseChars);
            if (XXPermissions.isHasPermission(context, Permission.READ_PHONE_STATE)){
                try {
                    LogUtils.e("===  play Content:" + orderVoice.getContent());
                    SpeechUtilOffline.getInstance(MyApplication.application).play(orderVoice, SpeechUtilOffline.PLAY_MODE.QUEUED);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            Log.e("tfbm", "PreferencesUtils content=" + content + "-----");
            List<String> idList;
            if (!TextUtils.isEmpty(ids)) {
                String[] idArray = ids.split(Constants.REGEX);
                idList = new ArrayList<>(Arrays.asList(idArray));
            } else {
                //ids为"" 直接创建集合
                idList = new ArrayList<>();
            }

            if (idList.size() >= Constants.ORDER_AMOUNT) {
                idList.remove(0);
                idList.add(orderId);
            } else {
                idList.add(orderId);
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < idList.size(); i++) {
                sb.append(idList.get(i));
                if (i < idList.size() - 1) {
                    sb.append(Constants.REGEX);
                }
            }

            if ("0".equals(payStatus)) {
                //从取消订单列表中
                PreferencesUtils.putString(MyApplication.application, Constants.PRE_STRING_CANCEL_ORDERS, sb.toString());
            } else {
                //发消息
                Log.e("lkf","payStatus"+payStatus);
                RxBus.getInstance().send(Events.EVENT_VOICE_BROADCAT, "");

                //发消息给子进程 H5 页面
                Intent intent = new Intent("updateH5Page");
                intent.putExtra("event", Events.EVENT_VOICE_BROADCAT);
                context.sendBroadcast(intent);

                //写入支付订单记录
                PreferencesUtils.putString(MyApplication.application, Constants.PRE_STRING_ORDERS, sb.toString());
            }
        }
    }
}
